//
//  NSDate+Extend.m
//  CoreCategory
//
//  Created by 成林 on 15/4/6.
//  Copyright (c) 2015年 沐汐. All rights reserved.
//

#import "NSData+MJUI.h"

@interface NSData ()

@end

@implementation NSData (MJUI)

- (NSDictionary *)mj_toDistionary{
    NSDictionary *responseDistionary = nil;
    if (self) {
        NSError *error = nil;
        responseDistionary = [NSJSONSerialization JSONObjectWithData:self options:NSJSONReadingMutableLeaves error:&error];
        if (error) {
            NSLog(@"json解析失败：%@",error);
            return nil;
        }
    }else{
        return  nil;
    }
    return  responseDistionary;
}

- (NSString *)mj_toUtf8String{
    NSString *string = [[NSString alloc] initWithData:self encoding:NSUTF8StringEncoding];
    if (string == nil) {
        string = [[NSString alloc] initWithData:[self mj_toUTF8Data] encoding:NSUTF8StringEncoding];
    }
    return string;
}

- (NSData *)mj_toUTF8Data{
    NSData *data = self;
    //保存结果
       NSMutableData *resData = [[NSMutableData alloc] initWithCapacity:data.length];
       
       //无效编码替代符号(常见 � □ ?)
       NSData *replacement = [@"�" dataUsingEncoding:NSUTF8StringEncoding];
       
       uint64_t index = 0;
       const uint8_t *bytes = data.bytes;
       
       while (index < data.length)
       {
           uint8_t len = 0;
           uint8_t header = bytes[index];
           
           //单字节
           if ((header&0x80) == 0)
           {
               len = 1;
           }
           //2字节(并且不能为C0,C1)
           else if ((header&0xE0) == 0xC0)
           {
               if (header != 0xC0 && header != 0xC1)
               {
                   len = 2;
               }
           }
           //3字节
           else if((header&0xF0) == 0xE0)
           {
               len = 3;
           }
           //4字节(并且不能为F5,F6,F7)
           else if ((header&0xF8) == 0xF0)
           {
               if (header != 0xF5 && header != 0xF6 && header != 0xF7)
               {
                   len = 4;
               }
           }
           
           //无法识别
           if (len == 0)
           {
               [resData appendData:replacement];
               index++;
               continue;
           }
           
           //检测有效的数据长度(后面还有多少个10xxxxxx这样的字节)
           uint8_t validLen = 1;
           while (validLen < len && index+validLen < data.length)
           {
               if ((bytes[index+validLen] & 0xC0) != 0x80)
                   break;
               validLen++;
           }
           
           //有效字节等于编码要求的字节数表示合法,否则不合法
           if (validLen == len)
           {
               [resData appendBytes:bytes+index length:len];
           }else
           {
               [resData appendData:replacement];
           }
           
           //移动下标
           index += validLen;
       }
       
       return resData;
}

+ (NSData *)mj_dataWithGIFName:(NSString *)name {
    NSURL *url = [[NSBundle mainBundle] URLForResource:name withExtension:@"gif"];
    NSData *imageData = [NSData dataWithContentsOfURL:url];
    return imageData;
}

@end
